package br.edu.univas.jobmanager.view;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableColumnModel;

import br.edu.univas.jobmanager.controller.ControllerPrincipal;
import br.edu.univas.jobmanager.entity.Categoria;
import br.edu.univas.jobmanager.entity.Tarefa;
import br.edu.univas.jobmanager.listener.FiltroListener;
import br.edu.univas.jobmanager.listener.ListenerEditarDeletarTarefa;
import br.edu.univas.jobmanager.utilitarios.JSearchTextField;
import br.edu.univas.jobmanager.utilitarios.Utilitarios;

public class PanelPrincipal extends JPanel {
	private static final long serialVersionUID = 1L;
	
	private JTable tarefasTable;
	private JLabel filtroLabel;
	private JComboBox filtroBox;
	private JSearchTextField buscaField;
	private JScrollPane scroll;
	
	private JLabel novaTarefaLabel;
	private JTextField novaTarefaField;
	private JButton novaTarefaButton;
	
	private ControllerPrincipal _controller;
	
	
	private ListenerEditarDeletarTarefa _listenerEditarDeletar;
	private FiltroListener _filtroListener; 
	public PanelPrincipal(ControllerPrincipal _controller) {
		this._controller = _controller;
		setLayout(new GridBagLayout());
		initialize();
		setBorder(BorderFactory.createTitledBorder("Tarefas"));
		this._listenerEditarDeletar = _controller.getEditarDeletarListener();
	}
	private void initialize() {
		add(getFiltroLabel(),createConstraints(0, 0, 1));
		add(getFiltroBox(),createConstraints(0,1, 1));
		
		add(getBuscaField(),createConstraints(1, 0, 3));
		
		add(getScroll(),createScrollConstraints());
		
		add(getNovaTarefaLabel(),createConstraints(6, 0, 1));
		add(getNovaTarefaField(),createConstraints(7, 0, 2));
		add(getNovaTarefaButton(),createConstraints(7, 2, 1));
		
	}
	public void setFiltroListener(FiltroListener _listener){
		this._filtroListener = _listener;
	}
	private GridBagConstraints createScrollConstraints(){
		GridBagConstraints gbc = createConstraints(2, 0, 3);
		gbc.gridheight = 4;
		return gbc;
	}
	
	private JTable getTarefasTable() {
		if(tarefasTable == null){
			tarefasTable = new JTable(_controller.getTarefasTableModel());
			tarefasTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
			tarefasTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			tarefasTable.getColumnModel().getColumn(0).setPreferredWidth(50);
			tarefasTable.getColumnModel().getColumn(1).setPreferredWidth(50);
			tarefasTable.getColumnModel().getColumn(2).setPreferredWidth(375);
			tarefasTable.getColumnModel().getColumn(3).setPreferredWidth(160);
			TableColumnModel column = tarefasTable.getColumnModel();
			TableCellRenderer renderer = new TableCellRenderer();
			column.getColumn(1).setCellRenderer(renderer);
			tarefasTable.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					if(e.getClickCount()==2){
						int linhaSelecionada = tarefasTable.getSelectedRow();
						Tarefa tarefa = _controller.getTarefasTableModel().getRow(linhaSelecionada);
						_listenerEditarDeletar.editarAction(tarefa);
					}
				}
			});
			tarefasTable.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					if(e.getKeyCode()==KeyEvent.VK_DELETE){
						int linhaSelecionada = tarefasTable.getSelectedRow();
						Tarefa tarefa = _controller.getTarefasTableModel().getRow(linhaSelecionada);
						int op = JOptionPane.showConfirmDialog(null, "Deseja deletar a tarefa:\n"+tarefa.getNome()+"?","Confirme", JOptionPane.YES_NO_OPTION);
						if(op==JOptionPane.YES_OPTION){
							_listenerEditarDeletar.deletarAction(tarefa);
						}
					}
				}
			});
		}
		return tarefasTable;
	}
	private JLabel getFiltroLabel() {
		if(filtroLabel == null){
			filtroLabel = new JLabel("Fitrar por: ");
		}
		return filtroLabel;
	}
	private JComboBox getFiltroBox() {
		if(filtroBox == null){
			filtroBox = new JComboBox(_controller.getCategoriaComboModel());
			filtroBox.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					_filtroListener.filtroAction((Categoria) filtroBox.getSelectedItem());
					
				}
			});
		}
		return filtroBox;
	}
	private JSearchTextField getBuscaField() {
		if(buscaField == null){
			buscaField = new JSearchTextField();
			buscaField.setIcon(Utilitarios.getIcon("proc.png"));
			buscaField.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent arg0) {
					_controller.filtrar(buscaField.getText());
				}
			});
		}
		return buscaField;
	}
	private JScrollPane getScroll() {
		if(scroll == null){
			scroll = new JScrollPane(getTarefasTable());
			scroll.setPreferredSize(new Dimension(400,300));
		}
		return scroll;
	}
	private JLabel getNovaTarefaLabel() {
		if(novaTarefaLabel == null){
			novaTarefaLabel = new JLabel("Nova Tarefa");
		}
		return novaTarefaLabel;
	}
	private JTextField getNovaTarefaField() {
		if(novaTarefaField == null){
			novaTarefaField = new JTextField(50);
			novaTarefaField.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent e){
					if(e.getKeyCode() == KeyEvent.VK_ENTER){
						String _nome = getNovaTarefaField().getText();		
						_controller.createAdicionarTarefa(_nome);
					}
				}
			});
		}
		return novaTarefaField;
	}
	private JButton getNovaTarefaButton() {
		if(novaTarefaButton == null){
			novaTarefaButton = new JButton("Nova");
			novaTarefaButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					String _nome = getNovaTarefaField().getText();		
					_controller.createAdicionarTarefa(_nome);
				}
			});
		}
		return novaTarefaButton;
	}
	public GridBagConstraints createConstraints(int linha, int coluna, int gridWidth){
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.insets = new Insets(5, 5, 5, 5);
		gbc.gridx = coluna;
		gbc.gridy = linha;
		gbc.gridwidth = gridWidth;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		return gbc;
	}
	public void cleanNomeField() {
		getNovaTarefaField().setText("");
		
	}
	public void atualizaCombo(Categoria _categoria){
		getFiltroBox().addItem(_categoria);
	}
}
